package com.chaoxuan.uebt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.Loading;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginActivity extends BaseActivity implements View.OnClickListener,TextWatcher {

    EditText editPhone,editPassword;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        editPhone=(EditText)findViewById(R.id.edit_phone);
        editPassword=(EditText)findViewById(R.id.edit_password);
        button=(Button)findViewById(R.id.btn_login);
        findViewById(R.id.btn_toregister).setOnClickListener(this);
        findViewById(R.id.btn_forget).setOnClickListener(this);
        findViewById(R.id.btn_login).setOnClickListener(this);
        initTitle();
        editPhone.addTextChangedListener(this);
        editPassword.addTextChangedListener(this);
        button.setSelected(true);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        final String phone=editPhone.getText().toString();
        String password=editPassword.getText().toString();
        if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(password)){
            button.setSelected(true);
        }else{
            button.setSelected(false);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_forget:
                startActivity(new Intent(this,ForgetActivity.class));
                break;
            case R.id.btn_toregister:
                startActivity(new Intent(this,RegisterActivity.class));
                break;
            case R.id.btn_login:
                final String phone=editPhone.getText().toString();
                String password=editPassword.getText().toString();
                if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(password))return;
                final Loading loading=new Loading(this,R.style.dialog);
                loading.show();
                subscribe(RetrofitUtils.getAPIService().login(phone,password, Const.DEVICE_TYPE),new BaseSubscriber<User>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        loading.dismiss();
                    }

                    @Override
                    public void onSuccess(User user) {
                        user.setUser_login(phone);
                        CacheUtils.getInstance(getBaseContext()).save(user);
                        Const.TAB=1;
                        startActivity(new Intent(getBaseContext(),MainActivity.class));
                        finish();
                    }
                });
                break;
        }
    }
}
